/*
 * UploadUiItem.java
 *
 * Created on 26 Haziran 2008 Perşembe, 22:20
 */

package com.pegaa.uploader.uploadui;

import com.pegaa.uploader.config.ConfigHolder;
import com.pegaa.uploader.event.FileUploadListener;
import com.pegaa.uploader.lang.Lang;
import com.pegaa.uploader.sender.Sender;
import com.pegaa.uploader.ui.filelist.item.ListItem;
import java.util.ArrayList;

/**
 * This component displays one item's status of upload, if item is not uploading
 * it shows just information about item, if item is uploading this component
 * shows status of upload.
 * 
 * @author  tayfun
 */
public class UploadItemUI extends javax.swing.JPanel implements FileUploadListener{
    
    private ConfigHolder configHolder = null;
    /**
     *  File to be uploaded by this component
     */
    private ListItem item = null;
    
    private ArrayList<FileUploadListener> fileUploadListeners = null;
    
    private float fileLengthInBytes = 0.0f;
    private long totalReadedBytes = 0;
    
    private Sender fileSender = null;
    private String targetID;
    
    /** Creates new form UploadUiItem */
    public UploadItemUI() {
        initComponents();
        this.fileUploadListeners = new ArrayList<FileUploadListener>(2);
    }
    
    public void setConfigHolder(ConfigHolder configHolder)
    {
        this.configHolder = configHolder;
        this.updateStrings();
    }
    
    private void updateStrings()
    {
        Lang lang = (Lang)this.configHolder.getObject("global.lang");
        this.labelFilePath.setText(lang.get("uploaduiitem.title"));
    }
    
    /**
     *  Sets the target folder id
     * 
     * @param targetID
     */
    public void setTargetFolderID(String targetID)
    {
        this.targetID = targetID;
    }
    
    /**
     * 
     * @param l
     */
    public void addFileUploadListener(FileUploadListener l)
    {
        this.fileUploadListeners.add(l);
    }
    
    /**
     *  Sets internal item
     * 
     * @param item
     */
    public void setItem(ListItem item)
    {
        this.item = item;
        this.updateComponents();
    }
    
    private void updateComponents()
    {
        this.jProgressBar1.setMaximum(100);
        this.jProgressBar1.setVisible(false);
        this.labelFilePathStr.setText(this.item.getPath());
    }
    
    /* Starts uploading of this component's item file */
    public void startUpload()
    {
        fileSender = new Sender(this.configHolder, this.targetID, this.item);
        fileSender.addFileUploadListener(this);
        fileSender.start();
    }
    
    /* Stops current upload */
    public void cancelUpload()
    {
        if(fileSender != null){
            fileSender.abort();
        }
    }
    
    private void notifyFileUploadFinished(int status){
        int len = this.fileUploadListeners.size();
        for(int i=0; i<len; i++)
        {
            this.fileUploadListeners.get(i).uploadFinished(status);
        }
    }
    
    public void uploadStarted(long fileSize) {
        fileLengthInBytes = fileSize * 1.0f;
        this.jProgressBar1.setVisible(true);
        
    }

    public void fileReaded(int readed) { 
        if(readed == -1){
            readed = 0;
        }
        this.totalReadedBytes += readed;
        int curPercentage = (int)((totalReadedBytes * 100) / fileLengthInBytes);
        this.jProgressBar1.setValue(curPercentage);
    }

    public void uploadFinished(int status) {
        this.jProgressBar1.setEnabled(false);
        this.notifyFileUploadFinished(status);
    }
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        labelFilePath = new javax.swing.JLabel();
        labelFilePathStr = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jProgressBar1 = new javax.swing.JProgressBar();

        setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        setPreferredSize(new java.awt.Dimension(152, 60));
        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel3.setPreferredSize(new java.awt.Dimension(100, 30));
        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
        jPanel3.add(labelFilePath);
        jPanel3.add(labelFilePathStr);

        jPanel2.add(jPanel3, java.awt.BorderLayout.NORTH);

        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        jProgressBar1.setMinimumSize(new java.awt.Dimension(10, 20));
        jProgressBar1.setPreferredSize(new java.awt.Dimension(148, 30));
        jPanel4.add(jProgressBar1);

        jPanel2.add(jPanel4, java.awt.BorderLayout.CENTER);

        add(jPanel2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JLabel labelFilePath;
    private javax.swing.JLabel labelFilePathStr;
    // End of variables declaration//GEN-END:variables


}
